package com.epro.dx.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.umeng.analytics.MobclickAgent;
import com.weipu.common.util.Logger;

public class BaseLoadAndEmptyDataFragment extends BaseFragment {
	//数据为空页面
	private LinearLayout emptyLayout;
	//请求错误页面
	protected LinearLayout errorLayout;
	//请求加载页面
	protected LinearLayout loadingLayout;
	//数据为空页面描述
	private TextView emptyContent;
	//错误类型标题
	private TextView errorTitle;
	//错误描述
	private TextView errorContent;
	//重试按钮
	protected Button retryBtn;
	
	private ImageView emptyIcon;
	
	private ImageView errorIcon;
    protected View inflate(int layoutResID)
    {
        View view = LayoutInflater.from(getActivity()).inflate(layoutResID, null);
        emptyLayout=(LinearLayout) view.findViewById(R.id.empty_layout);
        if(null!=emptyLayout){
        	emptyContent=(TextView) view.findViewById(R.id.empty_content);
        	emptyIcon=(ImageView) view.findViewById(R.id.empty_icon);
        }
        errorLayout=(LinearLayout) view.findViewById(R.id.error_layout);
        if(null!=errorLayout){
        	errorTitle=(TextView) view.findViewById(R.id.error_type);
        	errorIcon=(ImageView) view.findViewById(R.id.pic_empty);
        	errorContent=(TextView) view.findViewById(R.id.tv_empty);
        	retryBtn=(Button) view.findViewById(R.id.try_again);
        }
        loadingLayout=(LinearLayout) view.findViewById(R.id.loading_layout);
        return view;
    }
    
    /**
     * 展示加载页面
     * @param view
     */
    protected void showLoadingLayout(View view){
    	if(null!=view){
    		view.setVisibility(View.GONE);
    	}
    	if(null!=emptyLayout){
        	emptyLayout.setVisibility(View.GONE);
        }
        if(null!=errorLayout){
        	errorLayout.setVisibility(View.GONE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.VISIBLE);
        }
    }
    
    /**
     * 展示错误页面
     * @param view
     */
    protected void showErrorLayout(View view,int titleId,int contentId,int resId){
    	if(null!=view){
    		view.setVisibility(View.GONE);
    	}
    	if(null!=emptyLayout){
        	emptyLayout.setVisibility(View.GONE);
        }
        if(null!=errorLayout){
        	errorLayout.setVisibility(View.VISIBLE);
        	errorTitle.setText(titleId);
        	errorContent.setText(contentId);
        	errorIcon.setImageResource(resId);
        	retryBtn.setVisibility(View.VISIBLE);
        	errorTitle.setVisibility(View.VISIBLE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
    
    protected void showEmptyByErrorLayout(View view,int contentId,int resId){
    	if(null!=view){
    		view.setVisibility(View.GONE);
    	}
    	if(null!=emptyLayout){
        	emptyLayout.setVisibility(View.GONE);
        }
    	if(null!=errorLayout){
    		errorLayout.setVisibility(View.VISIBLE);
        	errorContent.setText(contentId);
        	errorIcon.setImageResource(resId);
        	retryBtn.setVisibility(View.GONE);
        	errorTitle.setVisibility(View.INVISIBLE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
    
    /**
     * 隐藏所有辅助界面
     * @param view
     */
    protected void hideAllLayout(){
    	if(null!=emptyLayout){
        	emptyLayout.setVisibility(View.GONE);
        }
        if(null!=errorLayout){
        	errorLayout.setVisibility(View.GONE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
    
    /**
     * 隐藏所有辅助界面 显示数据界面
     * @param view
     */
    protected void hideAllLayout(View view){
    	if(null!=view){
    		view.setVisibility(View.VISIBLE);
    	}
    	if(null!=emptyLayout){
        	emptyLayout.setVisibility(View.GONE);
        }
        if(null!=errorLayout){
        	errorLayout.setVisibility(View.GONE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
    
    
    
    /**
     * 空数据页面
     * @param view
     */
    protected void showEmptyLayout(View view,int stringId,int resId){
    	if(null!=view){
    		view.setVisibility(View.GONE);
    	}
    	if(null!=emptyLayout){
        	emptyLayout.setVisibility(View.VISIBLE);
        	emptyContent.setText(stringId);
        	emptyIcon.setImageResource(resId);
        }
        if(null!=errorLayout){
        	errorLayout.setVisibility(View.GONE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
    
    
    @Override
    public void onAttach(Activity activity)
    {
        Logger.d(this.getClass().getSimpleName(), "onAttach" + SystemClock.currentThreadTimeMillis());
        super.onAttach(activity);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Logger.d(this.getClass().getSimpleName(), "onCreate" + SystemClock.currentThreadTimeMillis());
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Logger.d(this.getClass().getSimpleName(), "onCreateView" + SystemClock.currentThreadTimeMillis());
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    
    @Override
    public void onResume()
    {
        super.onResume();
        //统计页面
        MobclickAgent.onPageStart(this.getTag());
    }
    
    @Override
    public void onPause()
    {
        super.onPause();
        //统计页面
        MobclickAgent.onPageEnd(this.getTag());
    }
    
    @Override
    public void onDestroyView()
    {
        Logger.d(this.getClass().getSimpleName(), "onDestroyView" + SystemClock.currentThreadTimeMillis());
        super.onDestroyView();
    }
    
}
